package ar.com.angrymachine.hunter;

import java.util.Vector;

public class ActivitySet
{
    public Vector activities = new Vector();
    public Vector globalActivities = new Vector();
    public String description;
    
    public ActivitySet(String description)
    {
    	this.description = description;
    }
    
    private void insertNewActivity(int where, Activity activity)
    {
    	activity.container = this;
   		activities.add(where, activity);
   		activity.onAddToActive(this);
    }
    
    public void addActivity(Activity activity)
    {
    	insertNewActivity(0, activity);
    }
    
    public void addActivity(Activity after, Activity activity)
    {
       int ndx = activities.indexOf(after);
       insertNewActivity(ndx+1, activity);
    }
    
	public void addActivityInTail(Activity activity)
	{
		insertNewActivity(activities.size(), activity);
	}
    
    public void replaceActivity(Activity former, Activity latter)
    {
        int ndx = activities.indexOf(former);
        if(ndx == -1) return;
        activities.setElementAt(latter, ndx);
        if(former == Game.current.getRunningActivity())
        {
            Game.current.resetActivity();
        }
        former.container = null;
        globalActivities.remove(former);
    }
    
    public void removeActivity(Activity activity)
    {
    	activity.onFinished();
    	activity.container = null;
        // if this was the currently activated activity, deselect it
        if(activity == Game.current.getRunningActivity())
        {
            Game.current.resetActivity();
        }
        activities.removeElement(activity);
    }
    
    private void updateActivity(int delta, Activity activity, Activity runningActivity)
    {
        //if the activity is complete, don't send an "action" flag
        boolean action = runningActivity == activity && !activity.isCompleted();
    	activity.update(delta, action, runningActivity != null);
    }

    public void update(int delta, Activity runningActivity)
    {
        Activity[] oldActivities = new Activity[activities.size()];
        activities.copyInto(oldActivities);
        // if the selected activity does not consume time, just run it
        // and don't run the others
        if(runningActivity != null && !runningActivity.consumesTime())
        {
            runningActivity.update(delta, true, true);
        }
        else
        {
            // update activities
            for (int i = 0; i < oldActivities.length; i++)
            {
                Activity activity = oldActivities[i];
                updateActivity(delta, activity, runningActivity);
            }
            // update special activities
            for (int j = 0; j < globalActivities.size(); j++)
            {
                Activity activity = (Activity) globalActivities.elementAt(j);
                updateActivity(delta, activity, runningActivity);            	
            }
        }
        // remove dead activities
        boolean changes = true;
        while(changes)
        {
            changes = false;
            for (int i = 0; i < activities.size(); i++)
            {
                Activity activity = (Activity) activities.elementAt(i);
                if(activity.isCompleted())
                {
                	removeActivity(activity);
                    changes = true;
                    break;
                }
            }
        }
    }
    
	public void onEnter()
	{
	}    
}
